// C1 E20 Wrong edition--do not konw the meaning of tab. --see mainANS.c
//
// NOT perfect, four ' ' can't stay neatly with one tab.
// So I use '*' instead of ' ' to check clearly.

#include <stdio.h>

#define MAXLINE 1000 // A game less than 1000.

int getstr(char s[], int lim);
int detab(char s[], int len);

int main()
{
    int len;
    char str[MAXLINE];

    int newlen;

    newlen = len = 0;

    while ((len = getstr(str, MAXLINE)) > 0) //len do not include '\0'
    {
        newlen = detab(str, len);
        printf(">>>\n%s\n", str);
        printf(">>>Source length = %d  New length = %d\n\n", len, newlen);
    }

    return 0;
}

int detab(char s[], int len)
{
    int c;
    int i, j, k;
    int nlen;

    i = j = k = 0;
    nlen = len;

    while (i < nlen)
    {
        if (s[i] == '\t')
        {
            nlen = nlen + 3; // Attention. nlen=nlen+3, but not nlen=len+3.

            if (nlen < MAXLINE)
                for (j = nlen; j >= i + 4; j--)
                    s[j] = s[j - 3];
            else
                printf("Exceed MAXLINE.\n");

            for (k = i; k < i + 4; k++)
                s[k] = '*';

            i = i + 4;
        }
        else
            i++;
    }

    return nlen;
}

int getstr(char s[], int lim)
{
    int c;
    int i;

    i = 0;
    lim = MAXLINE;

    while (i < lim - 1 && (c = getchar()) != EOF && c != '\n')
    {
        s[i] = c;
        i++;
    }
    if (c == '\n')
        s[i] = '\n',
        i++;
    s[i] = '\0';

    return i;
}